/**
 * Created by Administrator on 2017/12/11.
 */

import {queryOgaText,changeStatus,queryOrderFills,addOrderFills,querySingle,modConfirmTime,modPayTime,modShippingTime,modStatus} from "../services/order";
export default {
    namespace:'orderManage',
    state:{
        step: {
        },
        loading:true,
        stepFormSubmitting: false,
        data:{
            list:[],
            pagination:{},
        },
        optionInfoData:[],
        orderId:'',
    },
    effects:{
        *fetch({payload},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:true,
            });
            const response=yield call(queryOrderFills,payload);
            yield  put({
                type:'handleOrderManage',
                payload:response,
            })
            yield put({
                type:'OrderManageLoading',
                payload:false,
            })
        },
        *add({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:true,
            });
            const response=yield call(addOrderFills,payload);
            yield  put({
                type:'handleOrderManage',
                payload:response,
            })
            yield put({
                type:'OrderManageLoading',
                payload:false,
            })
            if (callback) callback(response);
        },
        *querySingle({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:true,
            });
            const response=yield call(querySingle,payload);
            yield  put({
                type:'handleOrderManage',
                payload:response,
            })
            yield put({
                type:'OrderManageLoading',
                payload:false,
            })
            if (callback) callback(response);
        },
        *keepData({payload, callback},{put,call}){
            yield put({
                type:'saveFormData',
                payload:true,
            });
            yield put({
                type:'saveFormData',
                payload:payload,
            });
            if (callback) callback(true);
        },
        *modConfirm({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            const response=yield call(modConfirmTime,payload);
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            if (callback) callback(response);
        },
        *modPay({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            const response=yield call(modPayTime,payload);
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            if (callback) callback(response);
        },
        *modShipping({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            const response=yield call(modShippingTime,payload);
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            if (callback) callback(response);
        },
        *modStatus({payload, callback},{put,call}){
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            const response=yield call(modStatus,payload);
            yield put({
                type:'OrderManageLoading',
                payload:payload,
            });
            if (callback) callback(response);
        },
        *queryOgaTextAndOptionInfo({payload,callback},{call,put}){
            const response=yield call(queryOgaText,payload);
            yield put({
                type:'handleOptionInfo',
                payload:response,
            })
            if(callback)callback(response);
        },
       * changeStatus({payload,callback},{call,put}){
            yield put({
                type:'changeOrderId',
                payload:payload,
            })
            const response=yield call(changeStatus,payload);
            if(callback)callback(response);
        },
    },
    reducers:{
        saveFormData(state, { payload }) {
                    return {
                        ...state,
                        FormData: {
                            ...state,
                            ...payload,
                        },
                loading:payload
            };
        },
        OrderManageLoading(state,{payload}){
            return ({
                ...state,
                loading:payload,
            })
        },
        handleOrderManage(state,{payload}){
            return ({
                ...state,
                data:payload.resultData.orderManage,
            })
        },
        saveConfirm(state,{payload}){
            return {
                ...state,
                step: {
                    ...state.step,
                    ...payload,
                },
                loading:payload
            };
        },
        handleOptionInfo(state,{payload}){
            return {
                ...state,
                optionInfoData:payload.resultData.optionInfo,
            }
        },
        changeOrderId(state,{payload}){
            return {
                ...state,
                orderId:payload.order_id,
            }
        }
        }

}